﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Space2D_Core;
using Space2D_Utils;

namespace Space2D_CoreTest
{
	class Program
	{
		static void Main(string[] args)
		{
			Int128 val = Int128.Parse("9223372036854775805223372036854775805");
			Console.WriteLine(val.ToString());
			val /= 92;
			Console.WriteLine(val.ToString());

			Int128 nod = MathUtils.NOD(Int128.Parse("1321546456489464565468"), Int128.Parse("13215464564894645654680"));

			WorldManager.Init(6.67545E-11, 1.0E+6, new Vector2d(10000000000, 10000000000));

			Int128Fraction G = Int128Fraction.FromDouble(6.67545E-11);
			Console.WriteLine(G.ToString() + " = " + G.ToDouble());

			G *= 10;
			Console.WriteLine(G.ToString() + " = " + G.ToDouble());

			PhysicalObject sun = new PhysicalObject("sun", 1.99E+27);
			sun.Init(new Vector2d(0, 0), new Vector2d(0, 0), new Vector2d(0, 0));
			sun.SetSize(7.0E+06, 7.0E+06);
			/*PhysicalObject sun1 = new PhysicalObject("sun", 1.99E+27);
			sun1.Init(new Vector2d(0, 0), new Vector2d(0, 10000), new Vector2d(5000000, 0));

			PhysicalObject sun2 = new PhysicalObject("sun", 1.99E+27);
			sun2.Init(new Vector2d(0, 0), new Vector2d(-8660, -5000), new Vector2d(-2500000, 4330000));

			PhysicalObject sun3 = new PhysicalObject("sun", 1.99E+27);
			sun3.Init(new Vector2d(0, 0), new Vector2d(8660, -5000), new Vector2d(-2500000, -4330000));*/

			PhysicalObject earth = new PhysicalObject("earth", 5.972E+21);
			earth.Init(new Vector2d(0, 0), new Vector2d(0, 30000), new Vector2d(150000000, 0));
			earth.SetSize(62500, 62500);

			PhysicalObject moon = new PhysicalObject("moon", 7.36E+16);
			moon.Init(new Vector2d(0, 0), new Vector2d(0, 31000), new Vector2d(150384000, 0));
			moon.SetSize(17500, 17500);

			PhysicalObject jupiter = new PhysicalObject("jupiter", 1.8986E+21);
			jupiter.Init(new Vector2d(0, 0), new Vector2d(0, 15070), new Vector2d(778547200, 0));
			jupiter.SetSize(715000, 715000);

			PhysicalObject saturn = new PhysicalObject("saturn", 5.6846E+23);
			saturn.Init(new Vector2d(0, 0), new Vector2d(0, 13500), new Vector2d(1433449400, 0));
			saturn.SetSize(602500, 602500);

			PhysicalObject uranus = new PhysicalObject("uranus", 8.6832E+22);
			uranus.Init(new Vector2d(0, 0), new Vector2d(0, 9000), new Vector2d(2876679100, 0));
			uranus.SetSize(255600, 255600);

			PhysicalObject neptun = new PhysicalObject("neptun", 1.0243E+23);
			neptun.Init(new Vector2d(0, 0), new Vector2d(0, 7500), new Vector2d(4503443700, 0));
			neptun.SetSize(250000, 250000);

			PhysicalObject ship = new Ship("ship", 10000, 3000, 3E+7, 1.5E+14);
			ship.Init(new Vector2d(0, 0), new Vector2d(0, 32500), new Vector2d(150065000, 0));
			ship.SetSize(10, 10);

			/*WorldManager.Register(sun1);
			WorldManager.Register(sun2);
			WorldManager.Register(sun3);*/
			WorldManager.Register(ship);

			WorldManager.Register(sun);
			WorldManager.Register(earth);
			WorldManager.Register(moon);
			WorldManager.Register(jupiter);
			WorldManager.Register(saturn);
			WorldManager.Register(uranus);
			WorldManager.Register(neptun);


			Random rand = new Random();

			for (int i = 0; i < 300; ++i)
			{
				PhysicalObject asteroid1 = new PhysicalObject("asteroid_1", 100000);
				asteroid1.Init(new Vector2d(0, 0), new Vector2d(0, 32500),
					new Vector2d(rand.Next(150060000, 150090000), rand.Next(-10000, 10000)));
				asteroid1.SetSize(192, 192);

				PhysicalObject asteroid2 = new PhysicalObject("asteroid_2", 100000);
				asteroid2.Init(new Vector2d(0, 0), new Vector2d(0, 32500),
					new Vector2d(rand.Next(150060000, 150090000), rand.Next(-10000, 10000)));
				asteroid2.SetSize(64, 64);

				PhysicalObject asteroid3 = new PhysicalObject("asteroid_3", 100000);
				asteroid3.Init(new Vector2d(0, 0), new Vector2d(0, 32500),
					new Vector2d(rand.Next(150060000, 150090000), rand.Next(-10000, 10000)));
				asteroid3.SetSize(256, 192);

				PhysicalObject asteroid4 = new PhysicalObject("asteroid_4", 100000);
				asteroid4.Init(new Vector2d(0, 0), new Vector2d(0, 32500),
					new Vector2d(rand.Next(150060000, 150090000), rand.Next(-10000, 10000)));
				asteroid4.SetSize(144, 111.5);

				WorldManager.Register(asteroid1);
				WorldManager.Register(asteroid2);
				WorldManager.Register(asteroid3);
				WorldManager.Register(asteroid4);
			}

			WorldManager.UpdateMinMaxSize();

			Stopwatch watch = new Stopwatch();
			int count =	WorldManager.GetWorldObjectsCount();
			long elapsed = 0;

			while (true)
			{
				watch.Start();
				WorldManager.Tick(10);
				watch.Stop();
				elapsed = watch.ElapsedMilliseconds;
				Console.WriteLine("Takes " + elapsed + " ms for " + count + ". FPS " + (1000/elapsed));
				watch.Restart();
			}
		}
	}
}
